package activity.main;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.laoxinwen.app.R;

import java.util.ArrayList;

import activity.NewsActivity;
import activity.TopicActivity;
import bean.News;
import bean.TabNewsItem;
import bean.TabTopicItem;
import bean.wraper.TabNewsListWrapper;
import common.BaseActivity;
import common.CommonHolder;
import common.Constants;
import common.FastAdapter;
import common.HttpCallBack;
import common.Requester;
import common.UserGuideUtil;
import util.bitmap.Img;

/**
 * Created by ilioili on 2015/3/17.
 */
public class FrgNewsList extends BaseFrgList {

    private ArrayList<TabNewsItem> dataList = new ArrayList<>();

    protected FastAdapter listAdapter = new FastAdapter() {
        @Override
        public int getCount() {
            return dataList.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TabNewsItem tabNewsItem = dataList.get(position);
            BaseActivity activity = (BaseActivity)getActivity();
            CommonHolder.BaseHolder holder = null;
            if (null == convertView) {
                switch (getItemViewType(position)) {
                    case 0:
                        convertView = getActivity().getLayoutInflater().inflate(R.layout.item_topic_origin, parent, false);
                        holder = new CommonHolder.TopicHolder(convertView, activity);
                        break;
                    case 1:
                        convertView = getActivity().getLayoutInflater().inflate(R.layout.item_news_with_no_pic, parent, false);
                        holder = new CommonHolder.NewsHolder(convertView, activity);
                        break;
                    case 2:
                        convertView = getActivity().getLayoutInflater().inflate(R.layout.item_news_with_one_pic, parent, false);
                        holder = new CommonHolder.NewsHolder(convertView, activity);
                        break;
                    case 3:
                        convertView = getActivity().getLayoutInflater().inflate(R.layout.item_news_with_three_pics, parent, false);
                        holder = new CommonHolder.NewsHolder(convertView, activity);
                        break;
                }
            } else {
                holder = (CommonHolder.BaseHolder) convertView.getTag();
            }
            if(holder instanceof CommonHolder.TopicHolder) {
                ((CommonHolder.TopicHolder) holder).setData(tabNewsItem.getTopic());
            }
            else {
                ((CommonHolder.NewsHolder) holder).setData(tabNewsItem);
            }
            //使用提示
            if(position == 1 && tabNewsItem.ableWa()) {
                UserGuideUtil.checkGuide(UserGuideUtil.GUIDE_MAIN_WA, ((MainActivity) activity).getRootView(), convertView.findViewById(R.id.bt_show_topic));
            }
            return convertView;
        }

        @Override
        public int getItemViewType(int position) {
            TabNewsItem tabNewsItem = dataList.get(position);
            if ("".equals(tabNewsItem.getId())) {
                return 0;
            } else if (null == tabNewsItem.getImgs() || tabNewsItem.getImgs().isEmpty()) {
                return 1;
            } else if (tabNewsItem.getImgs().size() < 3) {
                return 2;
            } else {
                return 3;
            }
        }

        @Override
        public int getViewTypeCount() {
            return 4;
        }
    };

    @Override
    protected String pageName() {
        return "新闻列表页";
    }

    @Override
    protected void requestData(final boolean isFresh) {
        Requester.getCurrentTabNewsList(bundle.getInt("tab"), pageIndex, new HttpCallBack<TabNewsListWrapper>() {
            @Override
            public void onSucceed(TabNewsListWrapper t) {
                TabNewsListWrapper newsGroupWraper = t;
                if (null != newsGroupWraper.getData()) {
                    if (pageIndex == 0) {
                        dataList.clear();
                    }
                    dataList.addAll(newsGroupWraper.getData());
                    if (pageIndex == 0) {
                        listView.setAdapter(listAdapter);
                    } else {
                        listAdapter.notifyDataSetChanged();
                    }
                }
                cancelFreshing();
                if (isFresh) {
                    lastFreshed = getCurrenTimeFlag();
                    proAbsListView.setLastFreshed(lastFreshed);
                }
            }

            @Override
            protected void onNetError() {
                super.onNetError();
                cancelFreshing();
            }

            @Override
            protected void onServerError(TabNewsListWrapper data) {
                super.onServerError(data);
                cancelFreshing();
            }

            private void cancelFreshing() {
                proAbsListView.cancelFreshing();
                proAbsListView.cancelLoading();
            }

        });

    }


}
